<!DOCTYPE html>
<!--

// Copyright 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

-->
<html>
<head>
<title></title>
<script src="../../../third_party/closure-library/closure/goog/base.js"></script>
<script>
// NOTE: base.js and all goog.require statements must be in JS script blocks
// NOTE: because they use document.write to add new script tags in place
goog.require('goog.testing.jsunit');
</script>
<script src="../../../src/traceur.js"></script>
</head>
<body>
<script>

var IdentifierToken = traceur.syntax.IdentifierToken;
var LiteralToken = traceur.syntax.LiteralToken;
var TokenType = traceur.syntax.TokenType;
var Token = traceur.syntax.Token;

var TreeWriter = traceur.outputgeneration.TreeWriter;
var trees = traceur.syntax.trees;

function testWriteStatement() {
  var tree = new trees.Program(
    'loc',
    [new trees.VariableStatement(
      'loc',
      new trees.VariableDeclarationList(
        'loc',
        'var',
        [new trees.VariableDeclaration(
          'loc',
          new trees.IdentifierExpression('loc', new IdentifierToken('loc', 'x')),
          new trees.LiteralExpression('loc', new LiteralToken(TokenType.NUMBER, '1', 'loc')))]
      )
    ),
    new trees.IfStatement(
      'loc',
      new trees.BinaryOperator(
        'loc',
        new trees.IdentifierExpression('loc', new IdentifierToken('loc', 'x')),
        new Token(TokenType.CLOSE_ANGLE, 'loc'),
        new trees.LiteralExpression('loc', new LiteralToken(TokenType.NUMBER, '0', 'loc'))
      ),
      new trees.Block(
        'loc',
        [new trees.PostfixExpression(
          'loc',
          new trees.IdentifierExpression('loc', new IdentifierToken('loc', 'x')),
          new Token(TokenType.PLUS_PLUS, 'loc')
        )]
      ),
      null
    )
  ]);
  var actual = TreeWriter.write(tree);

  var expected = '\nvar x = 1; \nif(x > 0) { \n  x ++ \n} \n';
  assertEquals(expected, actual);
}

var errorReporter = {
  reportError: function(position, message) {
    fail(message + ', ' + position);
  }
};

function parseAndWrite(name, source) {
  var sourceFile = new traceur.syntax.SourceFile(name, source);
  var scanner = new traceur.syntax.Scanner(errorReporter, sourceFile);
  var parser = new traceur.syntax.Parser(errorReporter, scanner);
  console.log('Parsing', name);

  var tree = parser.parseProgram();
  var result = TreeWriter.write(tree, false);
  console.log(result);
  return result;
}

function parseScriptAndWrite(scriptElement) {
  var source;
  var name;
  if (scriptElement.src) {
    name = scriptElement.src;
    var xhr = new XMLHttpRequest();
    // Synchronous load but these are already loaded when we get here so it
    // should be fast.
    xhr.open('GET', scriptElement.src, false);
    xhr.send();
    source = xhr.responseText;
  } else {
    name = 'inline-script';
    source = scriptElement.textContent;
  }
  parseAndWrite(name, source);
}

function testParserAndWriteAllScripts() {
  var scripts = document.querySelectorAll('script');
  for (var i = 0, script; script = scripts[i]; i++) {
    parseScriptAndWrite(script);
  }
}

function testParseAndWriteKeywords() {
  var result = parseAndWrite('test', 'x.case = 5;\n');
  assertEquals('\nx.case = 5; \n', result);
  var result = parseAndWrite('test', 'var obj = { var: 42 };\n');
  assertEquals('\nvar obj = { var: 42 }; \n', result);
}

</script>
</body>
</html>
